//
//  IWHWebViewVC.m
//  WangHuo
//
//  Created by zhangtongle on 2017/5/26.
//  Copyright © 2017年 ifeng. All rights reserved.
//

#import "IWHWebViewVC.h"

#import "IWHWebview.h"


@interface IWHWebViewVC () <IWHWebViewDelegate>
@property (nonatomic, assign) BOOL isNeedHideNavigation; //是否需要隐藏导航条
@property (nonatomic, copy) NSString *urlStr;
@property (nonatomic, strong) IWHWebview *webContainer;

@end

@implementation IWHWebViewVC
- (instancetype)initWithUrl:(NSString *)url {
    if (self = [super init]) {
        _urlStr = url;
        self.hidesBottomBarWhenPushed = YES;
        self.ignoreNav = NO;
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;

    CGFloat statusHeight ;
    if (self.ignoreNav) {
        statusHeight = 0;
    } else {
        statusHeight = UIApplication.sharedApplication.statusBarFrame.size.height;
    }
    IWHWebview *webContainer = [IWHWebview IWHWebview:CGRectMake(0, statusHeight, self.view.frame.size.width, self.view.ilg_height - statusHeight)];
    [self.view addSubview:webContainer];
    [webContainer navHidden:self.isNeedHideNavigation];
    webContainer.navTitleL.text = self.navTitle;
    self.webContainer = webContainer;
    webContainer.iwhDelegate = self;
    [webContainer setMainWebView];
    [webContainer loadRequest:_urlStr];

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"IWHWebViewVC-->url:%@", self.urlStr);
    
    [self isNeedHideNavigation];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - IWHWebViewDelegate
- (void)IWHWebViewDidClose {
    if (self.isPresented) {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)IWHShowAlertView:(UIAlertController *)alertViewVC  animated:(BOOL)animated {
    [self presentViewController:alertViewVC animated:YES completion:nil];
}

- (void)IWHOpenNewWindow:(NSString *)urlStr {
    _urlStr = urlStr;
    CGFloat statusHegiht = UIApplication.sharedApplication.statusBarFrame.size.height;
    IWHWebview *webContainer = [IWHWebview IWHWebview:CGRectMake(0, statusHegiht, [UIApplication sharedApplication].delegate.window.ilg_width, [UIApplication sharedApplication].delegate.window.ilg_height - statusHegiht)];
    webContainer.iwhDelegate = self;
    
    if ([UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController) {
        [[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController.view addSubview:webContainer];
 
    } else {
        [[UIApplication sharedApplication].delegate.window.rootViewController.view addSubview:webContainer];

    }
    [webContainer loadRequest:urlStr];
}

- (void)IWHMessageSomeone:(NSDictionary *)result {
    
}

#pragma mark - Other
- (BOOL)isNeedHideNavigation {
    if (self.isShowNav) {
        return NO;
    }
    self.navigationController.navigationBar.hidden = YES;

    BOOL resultValue = YES;
    
    return resultValue;
}

- (void)layoutNavigation {
    
}
@end
